package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.dao.SyLabelTypeMapper;
import com.eco.fanliapp.entity.SyLabelGoods;
import com.eco.fanliapp.entity.SyLabelType;
import com.eco.fanliapp.service.ISyLabelGoodsService;
import com.eco.fanliapp.service.ISyLabelTypeService;
import com.eco.fanliapp.utils.StringUtils;
import com.eco.fanliapp.vo.HaoDanKuItemInfo;
import com.eco.fanliapp.vo.respone.DTKResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 标签类型表 服务实现类
 */
@Service
public class SyLabelTypeServiceImpl extends ServiceImpl<SyLabelTypeMapper, SyLabelType> implements ISyLabelTypeService {

    private static final Logger log = LoggerFactory.getLogger(SyLabelTypeServiceImpl.class);

    @Autowired
    private SyLabelTypeMapper syLabelTypeMapper;

    @Autowired
    private ISyLabelGoodsService iSyLabelGoodsService;

    @Override
    public Integer insertSyLabelType(SyLabelType syLabelType) {
        Long id = syLabelType.getTableId();
        if (id != null) {
            syLabelType.setUpdateTime(new Date());
            return syLabelTypeMapper.updateById(syLabelType);
        } else {
            syLabelType.setCreateTime(new Date());
            syLabelType.setUpdateTime(new Date());
            syLabelType.setEnable(1);
            syLabelType.setDeleted(0);
            return syLabelTypeMapper.insert(syLabelType);
        }
    }

    @Override
    public Integer removeSyLabelType(SyLabelType syLabelType) {
        return syLabelTypeMapper.updateById(syLabelType);
    }


    @Override
    public List<SyLabelType> selectLabelName(String itemid) {
        return syLabelTypeMapper.selectLabelName(itemid);
    }

    @Override
    public List<SyLabelType> findLabelType(String labelName) {

        QueryWrapper<SyLabelType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("label_name", labelName);
        queryWrapper.eq("enable", 1);
        queryWrapper.eq("deleted", 0);
        List<SyLabelType> syLabelTypeList = syLabelTypeMapper.selectList(queryWrapper);
        return syLabelTypeList;
    }

    @Override
    public void savaDTKLabel(DTKResponse.DataBean itemData) {
        SyLabelGoods syLabelGoods = new SyLabelGoods();
        syLabelGoods.setItemId(itemData.getGoodsId());
        if (("1").equals(itemData.getTchaoshi())) {
            List<SyLabelType> syLabelTypeList = findLabelType("天猫超市");
            SyLabelGoods(syLabelTypeList, syLabelGoods);
        }
        if (("2").equals(itemData.getActivityType())) {
            List<SyLabelType> syLabelTypeList = findLabelType("淘抢购");
            SyLabelGoods labelGoods = iSyLabelGoodsService.selectGoodsLabel(itemData.getGoodsId(), "聚划算");
            if (syLabelTypeList.size() > 0) {
                if (labelGoods == null) {
                    SyLabelGoods(syLabelTypeList, syLabelGoods);
                } else {
                    //修改
                    labelGoods.setLabelTypeId(syLabelTypeList.get(0).getTableId().toString());
                    labelGoods.setLabelCode(syLabelTypeList.get(0).getLabelCode());
                    syLabelGoods.setUpdateTime(new Date());
                    iSyLabelGoodsService.updateById(labelGoods);
                }
            }
            // SyLabelGoods(syLabelTypeList, syLabelGoods);
        }
        if (("3").equals(itemData.getActivityType())) {
            List<SyLabelType> syLabelTypeList = findLabelType("聚划算");
            SyLabelGoods labelGoods = iSyLabelGoodsService.selectGoodsLabel(itemData.getGoodsId(), "淘抢购");
            if (syLabelTypeList.size() > 0) {
                if (labelGoods == null) {
                    SyLabelGoods(syLabelTypeList, syLabelGoods);
                } else {
                    //修改
                    labelGoods.setLabelTypeId(syLabelTypeList.get(0).getTableId().toString());
                    labelGoods.setLabelCode(syLabelTypeList.get(0).getLabelCode());
                    syLabelGoods.setUpdateTime(new Date());
                    iSyLabelGoodsService.updateById(labelGoods);
                }
            }
//            SyLabelGoods(syLabelTypeList, syLabelGoods);
        }
        if (("1").equals(itemData.getGoldSellers())) {
            List<SyLabelType> syLabelTypeList = findLabelType("金牌卖家");
            SyLabelGoods(syLabelTypeList, syLabelGoods);
        }
        if (("1").equals(itemData.getHaitao())) {
            List<SyLabelType> syLabelTypeList = findLabelType("淘淘商品");
            SyLabelGoods(syLabelTypeList, syLabelGoods);
        }
        if (("1").equals(itemData.getBrand())) {
            List<SyLabelType> syLabelTypeList = findLabelType("品牌产品");
            SyLabelGoods(syLabelTypeList, syLabelGoods);
        }
        if (itemData.getShopName() != null) {
            if (itemData.getShopName().contains("旗舰店")) {
                List<SyLabelType> syLabelTypeList = findLabelType("旗舰店");
                SyLabelGoods(syLabelTypeList, syLabelGoods);
            }
        }
        if (StringUtils.isNull(itemData.getActivityType()) || ("1").equals(itemData.getActivityType())) {
            List<SyLabelGoods> LabelGoods = iSyLabelGoodsService.findGoodsLabel(itemData.getGoodsId());
            if (LabelGoods.size() > 0 && LabelGoods.get(0) != null) {
                //删除商品标签
                LabelGoods.get(0).setDeleted(1);
                iSyLabelGoodsService.addSyLabelGoods(LabelGoods.get(0));
            }
        }
    }

    public void savaHDKLabel(HaoDanKuItemInfo.DataBean itemData) {
        SyLabelGoods syLabelGoods = new SyLabelGoods();
        syLabelGoods.setItemId(itemData.getItemid());
        if (itemData.getShopname() != null) {
            if (itemData.getShopname().contains("旗舰店")) {
                List<SyLabelType> syLabelTypeList = findLabelType("旗舰店");
                SyLabelGoods(syLabelTypeList, syLabelGoods);
            }
        }
        if (("1").equals(itemData.getIs_brand())) {
            List<SyLabelType> syLabelTypeList = findLabelType("品牌产品");
            SyLabelGoods(syLabelTypeList, syLabelGoods);
        }
        if (StringUtils.isNotNull(itemData.getActivity_type())) {
            if ("淘抢购".contains(itemData.getActivity_type())) {
                List<SyLabelType> syLabelTypeList = findLabelType("淘抢购");
                SyLabelGoods labelGoods = iSyLabelGoodsService.selectGoodsLabel(itemData.getItemid(), "聚划算");
                if (syLabelTypeList.size() > 0) {
                    if (labelGoods == null) {
                        SyLabelGoods(syLabelTypeList, syLabelGoods);
                    } else {
                        //修改
                        labelGoods.setLabelTypeId(syLabelTypeList.get(0).getTableId().toString());
                        labelGoods.setLabelCode(syLabelTypeList.get(0).getLabelCode());
                        syLabelGoods.setUpdateTime(new Date());
                        iSyLabelGoodsService.updateById(labelGoods);
                    }
                }
            }
            if ("聚划算".contains(itemData.getActivity_type())) {
                List<SyLabelType> syLabelTypeList = findLabelType("聚划算");
                SyLabelGoods labelGoods = iSyLabelGoodsService.selectGoodsLabel(itemData.getItemid(), "淘抢购");
                if (syLabelTypeList.size() > 0) {
                    if (labelGoods == null) {
                        SyLabelGoods(syLabelTypeList, syLabelGoods);
                    } else {
                        //修改
                        labelGoods.setLabelTypeId(syLabelTypeList.get(0).getTableId().toString());
                        labelGoods.setLabelCode(syLabelTypeList.get(0).getLabelCode());
                        syLabelGoods.setUpdateTime(new Date());
                        iSyLabelGoodsService.updateById(labelGoods);
                    }
                }
            }
        }
        if (("1").

                equals(itemData.getCuntao())) {
            List<SyLabelType> syLabelTypeList = findLabelType("村淘");
            SyLabelGoods(syLabelTypeList, syLabelGoods);
        }
        if (StringUtils.isNull(itemData.getActivity_type()) || itemData.getActivity_type().

                contains("普通活动")) {
            List<SyLabelGoods> LabelGoods = iSyLabelGoodsService.findGoodsLabel(itemData.getItemid());
            if (LabelGoods.size() > 0 && LabelGoods.get(0) != null) {
                //删除商品标签
                LabelGoods.get(0).setDeleted(1);
                iSyLabelGoodsService.addSyLabelGoods(LabelGoods.get(0));
            }
        }

    }

    public int SyLabelGoods(List<SyLabelType> syLabelTypeList, SyLabelGoods syLabelGoods) {
        int i = 0;
        if (syLabelTypeList.size() > 0) {
            syLabelGoods.setLabelTypeId(syLabelTypeList.get(0).getTableId().toString());
            syLabelGoods.setLabelCode(syLabelTypeList.get(0).getLabelCode());
            syLabelGoods.setCreateTime(new Date());
            syLabelGoods.setUpdateTime(new Date());
            syLabelGoods.setEnable(1);
            syLabelGoods.setDeleted(0);
            i = iSyLabelGoodsService.addSyLabelGoods(syLabelGoods);
        }
        return i;
    }

}
